<?php
namespace app\index\service;
use think\Db;
class  Question{

    //咨询类型
    function consultType(){
        $list = Db::name('consult_type')->field('consult_type_id,ct_name')->order('ct_sort desc,ct_addtime desc')->select();
        return $list;
    }

    //咨询列表
    function consultList($type,$tag,$sort){
        $paginate = 10;
        $join     = [
        /*    ['lt_user b','a.u_uid=b.u_uid','LEFT'],*/
            ['lt_consult_type c','a.consult_type_id=c.consult_type_id','LEFT'],
        ];
        $where['a.ct_state'] = 1;
        if(!empty($type)){
            $where['a.consult_type_id'] = $type;
        }
        if(!empty($tag)){
            $where['a.ct_title'] = array('like',"{$tag}%");
        }

        $order = 'a.ct_addtime desc';
        if(!empty($sort)){
            if($sort == 4){
                $order = 'a.ct_sum desc,a.ct_addtime desc';
            }elseif($sort == 5){
                $order = 'a.ct_count desc,a.ct_addtime desc';
            }
        }
        $info  = Db::name('consult')->alias('a')->join($join)->where($where)->field('a.*,c.ct_name')->order($order)->paginate($paginate);
        $page  = $info->render();
        $list  = $info->toArray();
        $join_a= [
            ['lt_admin b','a.u_uid=b.admin_id','LEFT'],
        ];
        $lt_user = session('lt_user');
        $collect = '';
        $user_collect  = Db::name('user_collect');
        $consult_reply = Db::name('consult_reply');
        if($lt_user){
           $where_q['ucc_state'] = 1;
           $where_q['u_uid']     = $lt_user['u_uid'];
           $collect = true;
        }
        foreach($list['data'] as $k=>$v){
           $list['data'][$k]['reply']   = '';
           $list['data'][$k]['collect'] = 0;
           if($v['ct_count'] > 0){
               $where_a['cr_type']    = 2;
               $where_a['consult_id'] = $v['consult_id'];
               $reply  = $consult_reply->alias('a')->join($join_a)->where($where_a)->field('a.cr_addtims,a.cr_details,b.a_fullname,b.a_img')->find();
               $list['data'][$k]['reply'] = $reply;
           }
           //查询用户收藏
           if($collect){
               $where_q['ucc_id']     = $v['consult_id'];
               $list['data'][$k]['collect'] = $user_collect->where($where_q)->count();
           }
        }
        $data['list'] = $list['data'];
        $data['page'] = $page;
        return $data;
    }

    //咨询详情
    function consultDetail(){
        $cid      = input('cid');
        $join     = [
            ['lt_user b','a.u_uid=b.u_uid','LEFT'],
            ['lt_consult_type c','a.consult_type_id=c.consult_type_id','LEFT'],
        ];
        $where['ct_state']   = 1;
        $where['consult_id'] = $cid;
        $data['data']    = Db::name('consult')->alias('a')->join($join)->where('consult_id',$cid)->field('a.*,b.u_nickname,b.u_phone,b.u_img,c.ct_name')->find();
        $lt_user = session('lt_user');
        $data['data']['collect'] = 0;
        if($lt_user){
            $where_q['ucc_state'] = 1;
            $where_q['u_uid']     = $lt_user['u_uid'];
            $where_q['ucc_id']     = $data['data']['consult_id'];
            $data['data']['collect'] = Db::name('user_collect')->where($where_q)->count();
        }

        $data['list']    = '';
        $data['count']   = '';
        if(!empty($data['data'])){
            $data['data']['u_nickname'] = $data['data']['u_nickname']?$data['data']['u_nickname']:substr_replace($data['data']['u_phone'],'****',3,4);
            //查询回答
            $data['list'] = Db::name('consult_reply')->where('consult_id',$data['data']['consult_id'])->field('consult_reply_id,u_uid,cr_type,cr_state,cr_details,cr_addtims')->order('cr_addtims asc')->select();
            if(!empty($data['list'])){
                foreach($data['list'] as $k=>$v){
                    if($v['cr_type'] == 1){
                        $data_user = Db::name('user')->where('u_uid',$v['u_uid'])->field('u_nickname,u_phone,u_img')->find();
                        $data['list'][$k]['nickname'] = $data_user['u_nickname']?$data_user['u_nickname']:substr_replace($data_user['u_phone'],'****',3,4);
                        $data['list'][$k]['img']      = $data_user['u_img'];
                    }else{
                        $data_user = Db::name('admin')->where('admin_id',$v['u_uid'])->field('a_fullname,a_img')->find();
                        $data['list'][$k]['nickname'] =  $data_user['a_fullname'];
                        $data['list'][$k]['img']      = $data_user['a_img'];
                    }
                }
            }
            $data['count'] = count($data['list']);
        }

        return   $data;
    }



    //新闻类型
    function newsType(){
        $list = Db::name('news_type')->field('news_type_id,nt_name')->order('nt_sort desc,nt_addtims desc')->select();
        return $list;
    }

    //新闻列表
    function newsList($type){
        if(!empty($type)){
            $where['a.news_type_id'] = $type;
        }
        $paginate = 6;
        $where['n_state'] = 2;
        $join  = [
            ['lt_news_type b','a.news_type_id=b.news_type_id','LEFT'],
        ];
        $field = 'a.n_title,a.news_id,a.n_describe,a.n_img,a.n_newstime,b.nt_name';
        $info  = Db::name('news')->alias('a')->join($join)->where($where)->field($field)->order('n_newstime desc')->paginate($paginate);
        $page  = $info->render();
        $data['list'] = $info;
        $data['page'] = $page;
        return $data;
    }

    //新闻详情
    function newsDetail(){
        $news   = Db::name('news');
        $nid    = input('nid');
        $join   = [
            ['lt_news_type b','a.news_type_id=b.news_type_id','LEFT'],
        ];
        $data['data']   = $news->alias('a')->join($join)->where('a.news_id',$nid)->field('a.news_id,a.n_title,a.n_source,a.n_content,a.n_newstime,b.nt_name')->find();

        $data['data']['up']     = $news->where('news_id','<',$data['data']['news_id'])->order('news_id desc')->value('news_id');//上一页
        $data['data']['below']  = $news->where('news_id','>',$data['data']['news_id'])->order('news_id asc')->value('news_id');//下一页
        return $data;
    }

    //推荐新闻
    function newsRecommend(){
        $list = Db::name('news')->field('n_title,n_describe,news_id')->order('n_addtime desc')->limit(9)->select();
        return $list;
    }

    //文书类型
    function writType(){
        $list = Db::name('writ_type')->field('writ_type_id,wt_name')->order('wt_sort desc,wt_addtime desc')->select();
        return $list;
    }

    //文书列表
    function writList($type,$tag,$sort){
        if(!empty($type)){
            $where['a.writ_type_id'] = $type;
        }
        if(!empty($tag)){
            $where['a.w_title'] = array('like',"{$tag}%");
        }
        $order = 'a.w_sort desc,a.w_addtime desc';
        if(!empty($sort)){
            if($sort == 3){
                $order = 'a.w_download_sum desc,a.w_addtime desc';
            }
        }
        $paginate = 15;
        $where['a.w_state'] = 2;
        $join  = [
            ['lt_writ_type b','a.writ_type_id=b.writ_type_id','LEFT'],
        ];
        $field = 'a.writ_id,a.w_title,a.w_describe,a.w_download_sum,a.w_writ_url,a.w_industry,a.w_copyright,a.w_addtime,b.wt_name';
        $info  = Db::name('writ')->alias('a')->join($join)->where($where)->field($field)->order($order)->paginate($paginate);
        $page  = $info->render();
        $data['list'] = $info;
        $data['page'] = $page;
        return $data;
    }



}
